package com.sevattal.service.leaves.controller;


import com.sevattal.common.base.result.R;
import com.sevattal.service.leaves.entity.Dept;
import com.sevattal.service.leaves.entity.vo.DeptVo;
import com.sevattal.service.leaves.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/admin/leaves/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @ApiOperation("新建部门")
    @PostMapping("createDept")
    public R  createDept(@ApiParam(value = "部门对象",required = true) @RequestBody Dept dept){
        boolean result = deptService.createDept(dept);
        if (result) {
            return R.ok().message("创建成功");
        }else{
            return R.error().message("创建失败");
        }
    }

    @ApiOperation("删除部门")
    @DeleteMapping("deleteDept/{id}")
    public R  deleteDept(@ApiParam(value = "部门ID",required = true) @PathVariable String id){
        boolean result = deptService.deleteDept(id);
        if (result) {
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("更新部门")
    @PutMapping("updateDept")
    public R  updateDept(@ApiParam(value = "部门对象",required = true) @RequestBody Dept dept){
        boolean result = deptService.updateDept(dept);
        if (result) {
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("根据id获取部门信息")
    @GetMapping("get/{id}")
    public R  getDept(@ApiParam(value = "部门ID",required = true) @PathVariable String id){
        DeptVo result = deptService.getByDeptId(id);
        return R.ok().data("dept",result);
    }

    @ApiOperation("获取部门信息列表")
    @GetMapping("getList")
    public R  getDeptList(){
        List<DeptVo> result = deptService.getDeptList();
        return R.ok().data("deptList" ,result);
    }
}

